package com.lingyistudio.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
/**
 * @ClassName: WebResponse
 * @Description:
 * @Author: cheng
 * @Date: 2019/5/11
 */
@ApiModel(description = "系统统一返回格式")

public class WebResponse<T> implements Serializable {
    /**
     * 返回编码
     */
    @ApiModelProperty("返回编码")
    private int code;

    /**
     * 消息描述
     */
    @ApiModelProperty("消息描述")
    private String msg;
    /**
     * 返回内容
     */
    @ApiModelProperty("返回内容")
    private T data;
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
    public T getData() {
        return data;
    }

    /**
     * 构建操作成功返回体
     *
     * @return 结果
     */
    public static WebResponse success() {
        return builder().code(WebResponseStatus.SUCCESS).build();
    }

    /**
     * 构建操作成功返回体
     * <p>
     * * @param msg  操作说明
     *
     * @return 结果
     */
    public static WebResponse success(String msg) {
        return builder().code(WebResponseStatus.SUCCESS).msg(msg).build();
    }

    /**
     * 构建操作成功返回体
     *
     * @param msg  操作说明
     * @param data 返回结果
     * @return 结果
     */
    public static <T> WebResponse success(String msg, T data) {
        return builder().code(WebResponseStatus.SUCCESS).msg(msg).data(data).build();
    }

    /**
     * 构建业务异常返回体
     *
     * @param msg 异常描述
     * @return
     */
    public static WebResponse bizError(String msg) {
        return builder().code(WebResponseStatus.BIZ_ERROR).msg(msg).build();
    }
    /**
     * 构建没有查询到数据返回体
     *
     * @return 结果
     */
    public static WebResponse notFound() {
        return builder().code(WebResponseStatus.NOT_FOUND).build();
    }

    /**
     * 构建没有查询到数据返回体
     *
     * @param msg 结果说明
     * @return 结果
     */
    public static WebResponse notFound(String msg) {
        return builder().code(WebResponseStatus.NOT_FOUND).msg(msg).build();
    }

    /**
     * 构建未登录错误返回体
     *
     * @param msg 错误说明
     * @return 结果
     */
    public static WebResponse noPermission(String msg) {
        return builder().code(WebResponseStatus.INVALID_TOKEN).build();
    }

    /**
     * 构建数据版本变化异常返回体
     *
     * @param msg 异常描述
     * @return
     */
    public static WebResponse errorVersion(String msg) {
        return builder().code(WebResponseStatus.ERROR).msg(msg).build();
    }

    //其他错误
    public static WebResponse errorOther(String msg) {
        return builder().code(WebResponseStatus.ERROR).msg(msg).build();
    }

    public static <T> WebResponse errorOther(String msg, T data) {
        return builder().code(WebResponseStatus.ERROR).msg(msg).data(data).build();
    }



    private WebResponse() {
    }

    private WebResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> AppResponseBuilder builder() {
        return new AppResponseBuilder<T>();
    }


    public static class AppResponseBuilder<T> {

        private int code;

        private String msg;

        private T data;

        public WebResponse build() {
            WebResponse appResponse = new WebResponse<>(this.code, this.msg, this.data);
            return appResponse;
        }

        public AppResponseBuilder code(WebResponseStatus status) {
            this.code = status.code;
            return this;
        }

        public AppResponseBuilder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public AppResponseBuilder data(T data) {
            this.data = data;
            return this;
        }
    }
}
